package jp.gr.java_conf.ryz.summarize.android.activity;

import java.util.List;

import jp.gr.java_conf.ryz.summarize.android.R;
import jp.gr.java_conf.ryz.summarize.android.logic.DataCreator;
import jp.gr.java_conf.ryz.summarize.android.model.Board;
import jp.gr.java_conf.ryz.summarize.android.model.Thred;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class SelectThreadActivity extends Activity
{
	public static final String PARAM_BOARD = "param_board";
	
	private Board _board;
	

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_thread);
		
		Bundle extras = getIntent().getExtras();
		_board = (Board)extras.getSerializable(PARAM_BOARD);
		
		init();
	}

	
	private void init()
	{
		final ListView listView = (ListView)findViewById(R.id.activity_select_thread_root);
		
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			@SuppressWarnings("unchecked")
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				ArrayAdapter<Thred> adapter = (ArrayAdapter<Thred>)parent.getAdapter();
				Thred thread = adapter.getItem(position);
				Intent intent = new Intent(SelectThreadActivity.this, ViewPostActivity.class);
				intent.putExtra(ViewPostActivity.PARAM_THREAD, thread);
				startActivity(intent);
			}
		});
		
		new AsyncTask<Board, Integer, List<Thred>>()
		{

			@Override
			protected List<Thred> doInBackground(Board... params)
			{
				List<Thred> threads = DataCreator.getThreads(params[0]);
				return threads;
			}

			@Override
			protected void onPostExecute(List<Thred> result)
			{
				ListAdapter adapter = new ArrayAdapter<Thred>(SelectThreadActivity.this, R.layout.thread_row, result);
				listView.setAdapter(adapter);
			}
			
		}.execute(_board);
	}
}
